@use "../_variables" as *;

.tool-details {
  margin: 10px 0;
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg-op-dark-extremeweak);

  .tool-summary {
    padding: 10px 15px;
    cursor: pointer;
    user-select: none;
    font-weight: 500;

    &:hover {
      background: var(--bg-op-dark-extremeweak);
    }
  }

  .tool-content {
    padding: 15px;
    border-top: 1px solid;
    background: var(--bg-op-dark-ultraweak);
  }
}

.message.received {
  blockquote {
    margin: 10px 0;
    padding: 10px 15px;
    border-radius: 8px;
    border-left: 4px solid var(--border-pri-blue);
    background: var(--bg-op-dark-extremeweak);

    h3 {
      margin: 0 0 10px 0;
      font-size: 16px;
      display: flex;
      align-items: center;
      gap: 8px;
    }

    pre {
      margin: 10px 0 0 0;
    }
  }
}

.tool-panel {
  margin: 10px 0;
  border-radius: 8px;
  overflow: hidden;

  summary {
    cursor: pointer;
    user-select: none;
    font-weight: 500;
    color: var(--text-inverted-weak);
  }

  .tool-content {
    padding: 15px;
    border-top: 1px solid;
    max-height: 300px;
    overflow: auto;
    border-color: var(--border-weak);

    pre {
      margin: 0;
      white-space: pre-wrap;
      word-break: break-all;
    }
  }
}

.setup-page {
  display: flex;
  flex: 1;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
  background: var(--bg-medium);
  width: 100%;
}

.setup-container {
  width: 100%;
  max-width: 600px;
  padding: 30px;
  border-radius: 12px;
  background: var(--bg-weak);
  box-shadow: 0 2px 10px var(--shadow-modal-light);

  h1 {
    margin: 0 0 10px;
    font-size: 24px;
  }

  .subtitle {
    margin-bottom: 30px;
    color: var(--text-weak);
  }

  .form-group {
    margin-bottom: 20px;

    label {
      display: block;
      margin-bottom: 8px;
      font-weight: 500;

      .required {
        color: var(--text-error);
        margin-left: 4px;
      }
    }

    .field-description,
    .field-model-description {
      font-size: 13px;
      margin-bottom: 8px;
      color: var(--text-weak);
    }
    .field-model-description {
      margin-bottom: -1rem;
    }

    input {
      width: 100%;
      padding: 8px 12px;
      border-radius: 6px;
      border: 1px solid var(--border);
      background: var(--bg-input);
      color: var(--text);
      transition: all 0.3s;

      &:focus {
        outline: none;
        border-color: var(--border-pri-blue);
        box-shadow: 0 0 0 2px var(--shadow-input);
      }

      &.error {
        border-color: var(--border-error);
      }
    }

    .error-message {
      margin-top: 4px;
    }
  }

  .error-message {
    color: var(--text-error);
    font-size: 14px;
  }

  > form > .error-message {
    cursor: pointer;
    word-break: break-all;
  }

  .form-actions {
    display: flex;
    gap: 12px;
    margin-top: 24px;

    button {
      flex: 1;
      padding: 12px;
      border: none;
      border-radius: 6px;
      font-size: 16px;
      cursor: pointer;
      transition: all 0.3s;
      display: flex;
      align-items: center;
      justify-content: center;

      &:disabled {
        opacity: 0.7;
        cursor: not-allowed;
      }

      .loading-spinner {
        width: 16px;
        height: 16px;
        border: 2px solid var(--border-inverted-op-mediumweak);
        border-radius: 50%;
        border-top-color: var(--border);
        animation: spin 0.8s linear infinite;
      }
    }

    .verify-btn {
      background: var(--bg-success);
      color: var(--text-light);

      &:hover:not(:disabled) {
        background: var(--bg-hover-success);
      }

      &:active:not(:disabled) {
        background: var(--bg-active-success);
      }
    }

    .submit-btn {
      background: var(--bg-pri-blue);
      color: var(--text-light);

      &:hover:not(:disabled) {
        background: var(--bg-hover-blue);
      }

      &:active:not(:disabled) {
        background: var(--bg-active-blue);
      }
    }
  }

  .provider-select {
    width: 100%;
    padding: 8px 12px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--bg-select-weak);
    transition: all 0.3s;
    cursor: pointer;
    color: var(--text);

    &:focus {
      outline: none;
      border-color: var(--border-pri-blue);
      box-shadow: 0 0 0 2px var(--shadow-input);
    }
  }

  .back-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    background: none;
    border: none;
    padding: 8px;
    margin-bottom: 16px;
    cursor: pointer;
    color: inherit;
    border-radius: 6px;

    &:hover {
      background: var(--bg-op-dark-extremeweak);
    }

    svg {
      fill: currentColor;
    }
  }
}

.tools-page {
  overflow: auto;

  .header-actions {
    display: flex;
    gap: 12px;
    margin-top: 16px;

    .edit-btn,
    .folder-btn,
    .reload-btn {
      display: flex;
      align-items: center;
      gap: 8px;
      height: 36px;
      width: fit-content;
      padding: 8px 16px;
      border: none;
      border-radius: 6px;
      font-size: 14px;
      cursor: pointer;
      transition: all 0.2s;

      svg {
        fill: currentColor;
      }
    }

    .edit-btn {
      background: var(--bg-pri-blue);
      color: var(--text-light);

      &:hover {
        background: var(--bg-hover-blue);
      }
    }

    .folder-btn {
      background: var(--bg-op-dark-extremeweak);

      &:hover {
        background: var(--bg-op-dark-ultraweak);
      }
    }

    .reload-btn {
      background: var(--bg-op-dark-extremeweak);

      img {
        height: 16px;
        width: 16px;
      }
    }
  }
}

[data-theme='dark'],
:root:has([data-theme='dark']) {
  .reload-btn {
    img {
      filter: invert(1);
    }
  }
}

.tools-container {
  width: 100%;
  max-width: 1152px;
  flex-direction: column;
  display: flex;
  justify-content: center;
  align-items: center;

  h1 {
    margin: 0 0 16px;
    font-size: 28px;
  }

  .subtitle {
    color: var(--text-weak);
    margin-bottom: 32px;
  }
}

.tools-list {
  width: 800px;

  .tool-section {
    margin-bottom: 16px;
    border-radius: 8px;
    overflow: hidden;
    background: var(--bg-medium);
    box-shadow: 0 2px 6px var(--shadow-modal-light);

    .tool-header {
      padding: 16px;
      cursor: pointer;
      display: flex;
      justify-content: space-between;
      align-items: center;

      &:hover {
        background: var(--bg-op-dark-extremeweak);
      }

      .tool-header-content {
        display: flex;
        align-items: center;
        gap: 12px;

        img, svg {
          width: 24px;
          height: 24px;
          opacity: 0.7;
        }

        svg {
          fill: var(--stroke-op-dark-extremestrong);
        }

        .tool-name {
          font-weight: 500;
        }
      }

      .tool-disabled-label {
        color: var(--text-weak);
        font-size: 14px;
        margin-left: 12px;
        display: flex;
        align-items: center;
      }

      .tool-toggle {
        width: 18px;
        font-size: 18px;
        transition: transform 0.3s;
        display: flex;
        align-items: center;
        justify-content: center;

        &.disabled {
          color: var(--stroke-neg-medium);
        }
      }

      .tool-edit-menu {
        width: 42px;
        height: 42px;
        padding: 2px;
        border-radius: 50%;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        opacity: 0;
        transition: opacity $transition-fast ease-in-out;

        &:hover,
        &[data-state="open"] {
          opacity: 1;
          background: var(--bg-op-dark-ultraweak);
        }
      }

      &:hover {
        .tool-edit-menu {
          opacity: 1;
        }
      }
    }

    .tool-content {
      display: none;
      padding: 0 16px 16px;
      max-height: 420px;
      overflow-y: auto;

      @include scrollbar;

      .tool-description {
        margin-bottom: 16px;
        color: var(--text-weak);
      }

      .sub-tool-error {
        color: var(--text-error);
        display: flex;
        align-items: flex-start;
        gap: 12px;
        padding: 12px;
        border-radius: 6px;
        background: var(--bg-error-light);
        max-height: 125px;
        overflow-y: auto;
        box-shadow: 0 1px 3px var(--shadow-modal-light);

        @include scrollbar;

        svg {
          width: 18px;
          height: 18px;
        }

        .sub-tool-error-text {
          flex: 1;
          font-size: 14px;

          .sub-tool-error-text-title {
            font-weight: 700;
            margin-bottom: 4px;
          }

          .sub-tool-error-text-content {
            color: var(--text-weak);
          }
        }
      }
    }

    &.expanded {
      .tool-toggle {
        transform: rotate(180deg);
      }

      .tool-content {
        display: block;
      }
    }

    &.disabled {
      .tool-header {
        cursor: default;

        &:hover {
          background: transparent;
        }
      }
    }

    .sub-tools {
      display: grid;
      gap: 12px;

      .sub-tool {
        padding: 12px;
        border-radius: 6px;
        background: var(--bg-op-dark-extremeweak);

        .sub-tool-name {
          font-weight: 500;
          margin-bottom: 4px;
        }

        .sub-tool-description {
          font-size: 14px;
          color: var(--text-weak);
        }
      }
    }
  }

  .error-message {
    padding: 16px;
    border-radius: 8px;
    background: #fff2f0;
    border: 1px solid #ffccc7;
    color: var(--text-error);
  }
}

.tool-switch-container {
  margin: 0 12px;
  display: inline-block;
  width: 48px;
  height: 24px;
}

.sub-tool {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .sub-tool-content {
    flex: 1;
  }
}

.tool-header {
  .tool-header-content {
    flex: 1;
  }
}

.tools-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 32px;
  position: relative;
  width: 100%;

  .header-actions {
    display: flex;
    gap: 12px;

    button {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 8px 16px;
      border: none;
      border-radius: 6px;
      cursor: pointer;
      transition: all 0.2s;
      font-size: 14px;
      color: var(--text);

      svg {
        fill: currentColor;
      }

      [data-theme='dark'] {
        background: rgba(255, 255, 255, 0.1);

        &:hover {
          background: rgba(255, 255, 255, 0.15);
        }
      }
    }

    .add-btn {
      background: var(--bg-success);
      color: var(--text-light);

      &:hover {
        background: var(--bg-hover-success);
      }

      [data-theme='dark'] {
        background: rgba(82, 196, 26, 0.2);

        &:hover {
          background: rgba(82, 196, 26, 0.3);
        }
      }
    }

    .edit-btn {
      background: var(--bg-pri-blue);
      color: var(--text-light);

      &:hover {
        background: var(--bg-hover-blue);
      }

      [data-theme='dark'] {
        background: rgba(24, 144, 255, 0.2);

        &:hover {
          background: rgba(24, 144, 255, 0.3);
        }
      }
    }

    .folder-btn {
      background: var(--bg-op-dark-extremeweak);

      &:hover {
        background: var(--bg-op-dark-ultraweak);
      }
    }
  }
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-overlay);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: $z-modal;
}

.modal-content {
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--bg-weak);
  border-radius: 8px;
  box-shadow: 0 2px 10px var(--shadow-modal);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border-weak);

  h2 {
    margin: 0;
    font-size: 20px;
  }

  .close-btn {
    background: none;
    border: none;
    font-size: 24px;
    color: inherit;
    cursor: pointer;
    padding: 4px;
    line-height: 1;
  }
}

.config-form {
  padding: 24px;

  .form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin-top: 24px;

    button {
      padding: 8px 16px;
      border: none;
      border-radius: 6px;
      cursor: pointer;
      transition: all 0.3s;
    }

    .cancel-btn {
      background: none;
      border: 1px solid var(--border);
      color: var(--text);

      &:hover {
        border-color: var(--border-hover-blue);
        color: var(--text-hover-blue);
      }
    }

    .submit-btn {
      background: var(--bg-pri-blue);
      color: var(--text-light);

      &:hover {
        background: var(--bg-hover-blue);
      }
    }
  }

  .cm-scroller {
    @include scrollbar;
  }
}

.config-textarea {
  width: 100%;
  min-height: 300px;
  font-family: monospace;
  padding: 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  resize: vertical;
  background: var(--bg-input);
  color: var(--text);

  &:focus {
    outline: none;
    border-color: var(--border-pri-blue);
    box-shadow: 0 0 0 2px var(--shadow-input);
  }
}

.tool-edit-popup-container {
  min-width: 860px;
  min-height: 631px;

  &.edit {
    min-width: 1110px;
  }

  .popup-confirm-content {
    padding: 0;
  }

  .tool-edit-popup {
    display: flex;
    flex: 1;
    height: 550px;

    .tool-edit-list {
      width: 250px;
      height: 631px;
      z-index: 1;
      overflow-y: auto;
      display: flex;
      flex-direction: column;
      gap: 10px;
      padding: 20px;
      background: var(--bg-medium);

      @include scrollbar;

      .tool-edit-list-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-radius: 4px;
        padding: 8px 10px;
        background: var(--bg);
        box-shadow: 0 2px 6px var(--shadow-modal-light);
        color: var(--text-ultraweak);
        cursor: pointer;
        transition: all 0.2s;

        &.active {
          background: var(--bg-op-dark-extremeweak);
        }

        &:hover {
          background: var(--bg-btn-hover);
        }

        &.error {
          color: var(--text-error);
          border: 1px solid var(--border-error);
        }

        label {
          display: flex;
          align-items: center;
          gap: 4px;
          cursor: pointer;
        }
      }
    }

    .tool-edit-popup-content {
      display: flex;
      flex-direction: column;
      flex: 1;
      padding: 20px;
    }

    .tool-edit-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 0;
      font-size: 20px;
      font-weight: 700;

      .tool-edit-header-actions {
        display: flex;
        align-items: center;
        gap: 10px;
      }
    }

    .tool-edit-content {
      display: flex;
      flex: 1;
      gap: 20px;

      .tool-edit-title {
        font-size: 18px;
        font-weight: 500;
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 12px 5px;

        .tool-edit-desc {
          font-size: 12px;
          font-weight: 400;
          color: var(--text-weak);
        }
      }

      .tool-edit-field {
        width: 300px;

        input,
        .select-button {
          width: 100%;
          height: 30px;
          border: none;
          border-radius: 10px;
          text-align: start;
          padding: 0 10px;
          background: var(--bg-op-dark-ultraweak);
          color: var(--text);
          font-size: 15px;

          &[type="number"] {
            -moz-appearance: textfield;
            appearance: textfield;
            grid-column: span 2;
          }

          &[type="number"]::-webkit-outer-spin-button,
          &[type="number"]::-webkit-inner-spin-button {
            -webkit-appearance: none;
            margin: 0;
          }
        }

        .field-content {
          display: flex;
          flex-direction: column;
          gap: 12px;
          width: 100%;
          height: 415px;
          padding: 0 5px;
          overflow-y: auto;

          @include scrollbar;

          .field-item {
            display: flex;
            flex-direction: column;
            gap: 4px;

            .field-item-array {
              display: flex;
              flex-direction: column;
              gap: 4px;
            }

            label {
              display: flex;
              justify-content: space-between;
              align-items: center;
              font-size: 15px;
              font-weight: 700;

              button {
                background: none;
                border: none;
                color: var(--text-weak);
                cursor: pointer;
                font-size: 14px;
                font-weight: 500;
                padding: 0;
                transition: all 0.2s;

                &:hover {
                  color: var(--text);
                }

                &:focus {
                  outline: none;
                }
              }
            }

            input,
            .select-button {
              &:focus {
                outline: none;
              }
            }
          }

          .field-item-array {
            border-top: 1px solid var(--stroke-dark-weak);

            &.no-border {
              border-top: none;
            }

            .field-item-array-item {
              display: flex;
              justify-content: space-between;
              align-items: center;
              gap: 5px;

              .key-input-wrapper {
                position: relative;

                .env-key-error {
                  position: absolute;
                  top: 0;
                  left: 0;
                  width: 100%;
                  height: 100%;
                  cursor: text;
                }
              }

              .field-item-array-item-clear {
                width: 20px;
                height: 20px;
                cursor: pointer;
                background: var(--bg-op-dark-ultraweak);
                border-radius: 50%;
                padding: 2px;
                flex-shrink: 0;

                &:hover {
                  background: var(--bg-op-dark-mediumweak);
                }

              }

              &.error {
                .env-key {
                  border: 2px solid var(--border-error);
                }
              }
            }

          }
        }
      }

      .tool-edit-json-editor {
        display: flex;
        flex-direction: column;
        flex: 1;
        width: 500px;
        position: relative;

        &.edit-json,
        &.add-json {
          min-width: 820px;
        }

        > [class^="cm-theme-"] {
          display: flex;
          flex: 1;

          .cm-editor {
            height: 415px;
            flex: 1;

            &.cm-focused {
              outline: none;
            }

            .cm-scroller {
              @include scrollbar;
            }
          }
        }

        .tool-edit-json-editor-copy {
          position: absolute;
          bottom: 0;
          right: 0;
          display: flex;
          align-items: center;
          gap: 10px;
          color: var(--text-weak);

          div {
            cursor: pointer;
            transition: all 0.2s;

            &:hover {
              color: var(--text);
            }
          }
        }
      }
    }
  }

  &.edit {
    .tool-edit-delete {
      margin-left: 250px;
    }
  }

  .tool-edit-delete {
    height: 40px;
    line-height: 40px;
    padding: 0 16px;
    background-color: var(--bg-cancel);
    box-shadow: 0 3px 7px var(--shadow-btn-cancel);
  }
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border-inverted-op-mediumweak);
  border-radius: 50%;
  border-top-color: var(--border);
  animation: spin 0.8s linear infinite;
}

.tool-edit-menu-item {
  display: flex;
  gap: 10px;

  svg {
    color: var(--stroke-dark-medium);
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.form-actions {
  button:disabled {
    opacity: 0.7;
    cursor: not-allowed;
  }
}

.global-loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-op-dark-ultraweak);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;

  .loading-spinner {
    width: 40px;
    height: 40px;
    border-width: 4px;
  }
}
